<template>
  <div class="rightbox" v-cloak>
    <h1>产品分类修改</h1>
    <div class="add-box" style="width:650px;">
      <el-form ref="form" :model="form" label-width="110px">
        <el-form-item
          label="排序序号"
          prop="sort"
          :rules="[
            { required: true, message: '不能为空', trigger: 'blur' }
          ]"
        >
          <el-input style="width:60px;" v-model="form.sort"></el-input>
        </el-form-item>
        <el-form-item
          label="科室名称"
          prop="name"
          :rules="[
            { required: true, message: '不能为空', trigger: 'blur' }
          ]"
        >
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item
          label="状态"
          prop="state"
          :rules="[
            { required: true, message: '不能为空', trigger: 'blur' }
          ]"
        >
          <el-radio v-model="form.state" label="1">启用</el-radio>
          <el-radio v-model="form.state" label="-1">禁用</el-radio>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit('form')">提交</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
import qs from 'qs'
export default {
  data () {
    return {
      form: {
        sort: '',
        name: '',
        state: ''
      },
      Departmentindex: ''
    }
  },
  methods: {
    getParams () {
      var that = this
      that.departmentindex = that.$route.params.departmentindex
      that.$pub.open()
      if (that.departmentindex) {
        axios.post('/gp/Department/Eidt', qs.stringify({
          departmentindex: that.departmentindex
        })
        )
          .then((res) => {
            that.$pub.close()
            if (res.data.code === 0) {
              that.form.sort = res.data.data.sort
              that.form.name = res.data.data.name
              that.form.state = res.data.data.state + ''
            } else {
              that.$pub.failcode(that, res.data.code, res.data.data)
            }
          })
          .catch(function () {
            that.$pub.catchbox(that)
          })
      } else {
        that.$router.push({
          name: 'proTypeList'
        })
      }
    },
    submit: function (form) {
      var that = this
      that.$pub.open()
      that.$refs[form].validate((valid) => {
        if (valid) {
          axios.post('/gp/Department/EidtPost', qs.stringify({
            Departmentindex: that.departmentindex,
            sort: that.form.sort,
            name: that.form.name,
            state: that.form.state
          })
          )
            .then((res) => {
              that.$pub.close()
              if (res.data.code === 0) {
                that.$alert('修改成功', '提示', {
                  confirmButtonText: '确定',
                  callback: action => {
                    that.$router.push({
                      name: 'proTypeList'
                    })
                  }
                })
              } else {
                that.$pub.failcode(that, res.data.code, res.data.data)
              }
            })
            .catch(function () {
              that.$pub.catchbox(that)
            })
        }
      })
    },
    resetForm: function () {
      var that = this
      that.form.sort = ''
      that.form.name = ''
    }
  },
  mounted () {
    this.getParams()
  }
}
</script>
